package com.borko.healthcare.tapestry.pages;

import org.apache.tapestry5.PersistenceConstants;
import org.apache.tapestry5.ValidationException;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SetupRender;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.services.Cookies;
import org.springframework.context.ApplicationContext;

import com.borko.healthcare.entities.beans.UserDetailsBean;
import com.borko.healthcare.entities.beans.enums.UserType;
import com.borko.healthcare.entities.interfaces.Pharmacist;
import com.borko.healthcare.entities.interfaces.Supplier;
import com.borko.healthcare.entities.interfaces.UserDetails;
import com.borko.healthcare.persistence.hibernate.services.PharmacistManager;
import com.borko.healthcare.persistence.hibernate.services.SupplierManager;

public class Login {
	
	@Inject
	private Cookies cookies;
	
	@Property
	@Persist(PersistenceConstants.FLASH)
	private String userNameField;
	
	@Property
	@Persist(PersistenceConstants.FLASH)
	private String passwordField;
	
	@Inject
	private ApplicationContext applicationContext;
	
	private SupplierManager supplierManager;
	private PharmacistManager pharmacistManager;
	
	@Persist
	private UserDetails userDetails;
	
	@Persist
	private int userId;
	
	@SetupRender
	public void initialize() {
		if (userDetails == null) {
			userDetails = new UserDetailsBean();
		}
	}
	
	public void onValidateFromLoginForm() throws ValidationException {
		if (userNameField == "" || passwordField == "") {
			throw new ValidationException("Please enter all fileds.");
		}
		
		supplierManager = (SupplierManager) applicationContext.getBean("supplierManager");
		
		for (Supplier supplier : supplierManager.list()) {
			if (supplier.getUserDetails().getPassword().equals(passwordField) 
					&& supplier.getUserDetails().getUserName().equals(userNameField)) {
				userDetails.setFullName(supplier.getUserDetails().getFullName());
				userId = supplier.getId();
				userDetails.setUserType(UserType.SUPPILER);
				return;
			}
		}
		
		pharmacistManager = (PharmacistManager) applicationContext.getBean("pharmacistManager");
		
		for (Pharmacist pharmacist : pharmacistManager.list()) {
			if (pharmacist.getUserDetails().getPassword().equals(passwordField) 
					&& pharmacist.getUserDetails().getUserName().equals(userNameField)) {
				userDetails.setFullName(pharmacist.getUserDetails().getFullName());
				userId = pharmacist.getId();
				userDetails.setUserType(UserType.PHARMACIST);
				return;
			}
		}
		
		throw new ValidationException("Invalid username or password.");
	}
	
	public Object onSuccessFromLoginForm() {
		cookies.writeCookieValue("isUserLoged", "true");
		cookies.writeCookieValue("userId", userId + "");
		cookies.writeCookieValue("userFullName", userDetails.getFullName());
		cookies.writeCookieValue("userType", userDetails.getUserType().toString().toLowerCase());
		
		return Welcome.class;
	}
	

}
